using System;
using System.Collections.Generic;
using NopSolutions.NopCommerce.BusinessLogic.Products;

namespace NopSolutions.NopCommerce.LuceneHelper.Tasks
{
    public class ProductsUpdateTask : BaseTask<ProductsUpdateTask>
    {
        #region Properties

        public bool ForceUpdate { get; set; }

        #endregion

        #region Constructors

        public ProductsUpdateTask(bool forceUpdate)
        {
            ForceUpdate = forceUpdate;
        }

        #endregion

        #region Task

        public override void DoTask(IndexManager indexManager, SearchManager searchManager, IndexWriterWrapper indexer)
        {
            foreach (var task in ProductManager.GetAllProducts().ToTasks(ForceUpdate))
            {
                task.DoTask(indexManager, searchManager, indexer);
            }
        }

        public override LuceneIndexType IndexType
        {
            get { return LuceneIndexType.Product; }
        }

        public override bool RebuildIndex
        {
            get { return true; }
        }

        public override IEnumerable<Func<ProductsUpdateTask, object>> ComparableProperties()
        {
            return new List<System.Func<ProductsUpdateTask, object>> { x => x.ForceUpdate };
        }

        public override void UpdateTask(ProductsUpdateTask newTask)
        {
            if (!ForceUpdate)
            {
                ForceUpdate = newTask.ForceUpdate;
            }
        }

        #endregion

    }
}
